//
//  BasePageListFetcher.swift
//  SwiftScienceChina
//
//  Created by 贾培军 on 2020/1/9.
//  Copyright © 2020 贾培军. All rights reserved.
//

import Foundation

public protocol SWPageListRequestDelegate : NSObjectProtocol {
    func requestWillRefresh()
    func requestEndRefreshWithError(error: NSError?)
    func requestWillFetchMore()
    func requestEndFetchMoreWithError(error: NSError)
    func requestTextureWillFetchMore()
    
}

open class SWBasePageListFetcher: NSObject, SWPageListRequestDelegate {
    open func requestEndRefreshWithError(error: NSError) {
        
    }
    
    open func requestEndFetchMoreWithError(error: NSError) {
        
    }
    
    open func requestTextureWillFetchMore() {
        
    }
    
    public var pageSize = 10
    public var pageNum = 1
    public var lastID = "0"
    public var prePageNum = 1
    public typealias CompleteBlock = (_ total: NSInteger, _ retItemArray: NSArray?, _ error: NSError?)->Void
    public var completeBlock:CompleteBlock? = nil
    
    open func stop() {
        
    }
    
    open func startWithBlock(completeBlock: @escaping CompleteBlock) {
        self.completeBlock = completeBlock
    }
    open func requestWillRefresh() {
        prePageNum = pageNum
        pageNum = 1
        lastID = "0"
    }
    open func requestEndRefreshWithError(error: NSError?) {
        if error != nil {
            pageNum = prePageNum
        }
    }
    open func requestWillFetchMore() {
        pageNum+=1
    }
}
